﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace Star.Tools.Excel.Extensions
{
    public  class CellStyleExtensions
    {
        public static readonly  CellStyleExtensions Instance = new CellStyleExtensions();
        private ICellStyle _cellStyle;
        private IFont _font;

        //public static CellStyleExtensions Instance { get; private set; } = null;

        //static CellStyleExtensions()
        //{
        //    Instance = new CellStyleExtensions();
        //}

        //XSSFCellStyle StyleColor = (XSSFCellStyle)workbook.CreateCellStyle();
        //XSSFColor xssfColor = new XSSFColor();
        //byte[] colorRgb = { (byte)95, (byte)155, (byte)213 };
        //xssfColor.SetRgb(colorRgb);
        //    StyleColor.FillForegroundColorColor = xssfColor;
        //    StyleColor.FillPattern = FillPattern.SolidForeground;
        public CellStyleExtensions()
        {

        }
        /// <summary>
        /// 获取枚举对应CellStyle
        /// </summary>
        /// <param name="excelStyle">Excel样式枚举</param>
        /// <returns></returns>
        public static ICellStyle GetCellStyle(ICellStyle _cellStyle, ExcelCellStyleEnum excelStyle)
        {
            switch (excelStyle)
            {

                case ExcelCellStyleEnum.left:
                    _cellStyle.Alignment = HorizontalAlignment.Left;
                    break;
                case ExcelCellStyleEnum.right:
                    _cellStyle.Alignment = HorizontalAlignment.Right;
                    break;
                case ExcelCellStyleEnum.center:
                    _cellStyle.Alignment = HorizontalAlignment.Center;
                    break;

                default:
                    throw new ArgumentException("参数无效");
            }
            return _cellStyle;
        }
    
        public void CreateCellStyle(IWorkbook workbook)
        {
            _cellStyle = workbook.CreateCellStyle();
            _font = workbook.CreateFont();

        }

        public  ICellStyle GetTitleStyle(IWorkbook workbook)
        {
            CreateCellStyle(workbook);
            SetBorderStyle();
            SetContentStyle();
            SetFontStyle();
            return _cellStyle;
        }
        public ICellStyle GetContentStyle(IWorkbook workbook)
        {
            CreateCellStyle(workbook);
            SetBorderStyle();
            _cellStyle.Alignment = HorizontalAlignment.Center;//水平对齐
            _cellStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中
            _cellStyle.FillPattern = FillPattern.SolidForeground;
            _cellStyle.FillForegroundColor = HSSFColor.White.Index;
            _font.FontName = "微软雅黑";
            _font.FontHeightInPoints = 11;
            _font.Boldweight = (short)FontBoldWeight.None;
            _cellStyle.SetFont(_font);
        
            return _cellStyle;
        }
        public ICellStyle GetContentStyle(IWorkbook workbook, object value)
        {
            CreateCellStyle(workbook);
            SetBorderStyle();
            _cellStyle.Alignment = HorizontalAlignment.Center;//水平对齐
            _cellStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中
            _cellStyle.FillPattern = FillPattern.SolidForeground;
            _cellStyle.FillForegroundColor = HSSFColor.White.Index;
            _font.FontName = "微软雅黑";
            _font.FontHeightInPoints = 11;
            _font.Boldweight = (short)FontBoldWeight.None;
            _cellStyle.SetFont(_font);

            if (value != null)
            {

            }
            return _cellStyle;
        }

        private void SetBorderStyle()
        {
            _cellStyle.BorderTop = BorderStyle.Thin;
            _cellStyle.BorderRight = BorderStyle.Thin;
            _cellStyle.BorderLeft = BorderStyle.Thin;
            _cellStyle.BorderBottom = BorderStyle.Thin;
        }


        private void SetBorderStyle(ICellStyle cellStyle)
        {
            cellStyle.BorderTop = BorderStyle.Thin;
            cellStyle.BorderRight = BorderStyle.Thin;
            cellStyle.BorderBottom = BorderStyle.Thin;
            cellStyle.BorderLeft = BorderStyle.Thin;
        }
        private void SetContentStyle()
        {
            _cellStyle.Alignment = HorizontalAlignment.Center;//水平对齐
            _cellStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中
            _cellStyle.FillForegroundColor =HSSFColor.Grey25Percent.Index;
            _cellStyle.FillPattern = FillPattern.SolidForeground;
        }
        
        private void SetFontStyle()
        {
            _font.FontName = "微软雅黑";
            _font.Boldweight = (short)FontBoldWeight.Bold;
            _font.FontHeightInPoints = 11;
            _font.Color = HSSFColor.White.Index;
            _cellStyle.SetFont(_font);
        }


        #region 单元格边框样式

        private static ICellStyle GetCellBorderStyle(int style, IWorkbook workbook, BorderStyle outStyle, BorderStyle inStyle)
        {
            ICellStyle cellStyle = workbook.CreateCellStyle();

            cellStyle.BorderTop = inStyle;
            cellStyle.BorderTop = inStyle;
            cellStyle.BorderTop = inStyle;
            cellStyle.BorderTop = inStyle;
            switch (style)
            {
                //九宫格中间的样式
                case 0:
                    break;

                //九宫格左上角样式
                case 1:
                    cellStyle.BorderTop = outStyle;
                    cellStyle.BorderLeft = outStyle;
                    break;

                //九宫格上方样式
                case 2:
                    cellStyle.BorderTop = outStyle;
                    break;

                //九宫格右上角样式
                case 3:
                    cellStyle.BorderTop = outStyle;
                    cellStyle.BorderRight = outStyle;
                    break;

                //九宫格右边样式
                case 4:
                    cellStyle.BorderRight = outStyle;
                    break;

                //九宫格右下角样式
                case 5:
                    cellStyle.BorderBottom = outStyle;
                    cellStyle.BorderRight = outStyle;
                    break;

                //九宫格下方样式
                case 6:
                    cellStyle.BorderBottom = outStyle;
                    break;

                //九宫格左下角样式
                case 7:
                    cellStyle.BorderBottom = outStyle;
                    cellStyle.BorderLeft = outStyle;
                    break;

                //九宫格左边样式
                case 8:
                    cellStyle.BorderLeft = outStyle;
                    break;

                //单行单列样式
                case 9:
                    cellStyle.BorderTop = outStyle;
                    cellStyle.BorderLeft = outStyle;
                    cellStyle.BorderRight = outStyle;
                    cellStyle.BorderBottom = outStyle;
                    break;

                //单列多行上方样式
                case 10:
                    cellStyle.BorderTop = outStyle;
                    cellStyle.BorderLeft = outStyle;
                    cellStyle.BorderRight = outStyle;
                    break;
                //单列多行中间样式

                case 11:
                    cellStyle.BorderLeft = outStyle;
                    cellStyle.BorderRight = outStyle;
                    break;

                //单列多行下方样式
                case 12:
                    cellStyle.BorderLeft = outStyle;
                    cellStyle.BorderRight = outStyle;
                    cellStyle.BorderBottom = outStyle;
                    break;

                //单行多列右边样式
                case 13:
                    cellStyle.BorderTop = outStyle;
                    cellStyle.BorderRight = outStyle;
                    cellStyle.BorderBottom = outStyle;
                    break;

                //单行多列中间样式
                case 14:
                    cellStyle.BorderTop = outStyle;
                    cellStyle.BorderBottom = outStyle;
                    break;

                //单行多列下方样式
                case 15:
                    cellStyle.BorderTop = outStyle;
                    cellStyle.BorderLeft = outStyle;
                    cellStyle.BorderBottom = outStyle;
                    break;

                default:
                    break;
            }
            return cellStyle;
        }

        #endregion 单元格边框样式

        /// <summary>
        /// 表头样式
        /// </summary>
        /// <param name="workbook"></param>
        /// <param name="sheet"></param>
        /// <param name="headerRow"></param>
        public  ICellStyle SetHeadStyle(IWorkbook workbook)
        {
            CreateCellStyle(workbook);
            SetBorderStyle();
           
            //背景
            _cellStyle.FillPattern = FillPattern.SolidForeground;
            _cellStyle.FillForegroundColor = 0;
            ((XSSFColor)_cellStyle.FillForegroundColorColor).SetRgb(new byte[] { 146, 208, 80 });

            //字体
            var font = workbook.CreateFont();
            font.FontName = "微软雅黑";
            font.Boldweight = (short)FontBoldWeight.Bold;
            font.FontHeightInPoints = 12;
            _cellStyle.SetFont(font);

            //居中
            _cellStyle.Alignment = HorizontalAlignment.Center;
            _cellStyle.VerticalAlignment = VerticalAlignment.Center;
            return _cellStyle;
        }
      
    }
   
   
}
